#include "wc.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>

void init_counts(CountResult *result) {
    result->char_count = 0;
    result->word_count = 0;
    result->sent_count = 0;
}

int count_chars(const char *filename) {
    FILE *file = fopen(filename, "r");
    if (!file) {
        perror("Error opening file");
        return -1;
    }

    int count = 0;
    int c;
    while ((c = fgetc(file)) != EOF) {
        count++;
    }

    fclose(file);
    return count;
}

int count_words(const char *filename) {
    FILE *file = fopen(filename, "r");
    if (!file) {
        perror("Error opening file");
        return -1;
    }

    int count = 0;
    int in_word = 0;
    int c;

    while ((c = fgetc(file)) != EOF) {
        if (isalpha(c)) {
            if (!in_word) {
                count++;
                in_word = 1;
            }
        } else {
            in_word = 0;
        }
    }

    fclose(file);
    return count;
}

int count_sentences(const char *filename) {
    FILE *file = fopen(filename, "r");
    if (!file) {
        perror("Error opening file");
        return -1;
    }

    int count = 0;
    int c;

    while ((c = fgetc(file)) != EOF) {
        if (c == '.' || c == '!' || c == '?') {
            count++;
        }
    }

    fclose(file);
    return count;
}

void count_all(const char *filename, CountResult *result) {
    FILE *file = fopen(filename, "r");
    if (!file) {
        perror("Error opening file");
        return;
    }

    int c;
    int in_word = 0;
    
    while ((c = fgetc(file)) != EOF) {
        // Count characters
        result->char_count++;
        
        // Count words
        if (isalpha(c)) {
            if (!in_word) {
                result->word_count++;
                in_word = 1;
            }
        } else {
            in_word = 0;
        }
        
        // Count sentences
        if (c == '.' || c == '!' || c == '?') {
            result->sent_count++;
        }
    }

    fclose(file);
}

int main(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s [option] [filename]\n", argv[0]);
        fprintf(stderr, "Options:\n");
        fprintf(stderr, "  -c    Count characters\n");
        fprintf(stderr, "  -w    Count words\n");
        fprintf(stderr, "  -s    Count sentences\n");
        fprintf(stderr, "  -all  Count all three metrics\n");
        return 1;
    }

    CountResult result;
    init_counts(&result);

    if (strcmp(argv[1], "-c") == 0) {
        result.char_count = count_chars(argv[2]);
        printf("Character count: %d\n", result.char_count);
    } 
    else if (strcmp(argv[1], "-w") == 0) {
        result.word_count = count_words(argv[2]);
        printf("Word count: %d\n", result.word_count);
    } 
    else if (strcmp(argv[1], "-s") == 0) {
        result.sent_count = count_sentences(argv[2]);
        printf("Sentence count: %d\n", result.sent_count);
    } 
    else if (strcmp(argv[1], "-all") == 0) {
        count_all(argv[2], &result);
        printf("Character count: %d\n", result.char_count);
        printf("Word count: %d\n", result.word_count);
        printf("Sentence count: %d\n", result.sent_count);
    } 
    else {
        fprintf(stderr, "Invalid option: %s\n", argv[1]);
        return 1;
    }

    return 0;
}